package com.heima.common;

import java.util.Random;

public class IdGenerator {

    /**
     * 生成一个18位的数字ID，首位不为0，返回值为Long类型
     * @return 18位ID Long类型
     */
    public static Long generate18DigitId() {
        Random random = new Random();

        // 首位数字，范围是1-9，确保不为0
        long firstDigit = random.nextInt(9) + 1;

        // 生成剩余的18位数字，范围是0-9
        StringBuilder idBuilder = new StringBuilder();
        idBuilder.append(firstDigit);
        for (int i = 0; i < 17; i++) {
            idBuilder.append(random.nextInt(10));
        }

        // 将生成的ID字符串转换为Long类型
        try {
            return Long.parseLong(idBuilder.toString());
        } catch (NumberFormatException e) {
            throw new RuntimeException("生成的ID超过了Long的范围", e);
        }
    }
}
